const fs = require("fs")
const path = require("path")
const axios = require("axios")
const cheerio = require("cheerio")

/**
 * 本脚本用于下载国内、国际所有的城市代码或者参数数据。
 * 如不需要更新城市信息，只需执行一次即可。
 */


//装载国内城市
let city = {}
const baseApi = "https://www.tianqi.com/"
//请求地址国内城市接口
const chinacityApi = "https://www.tianqi.com/chinacity.html"
const apiArray = [
    "https://www.tianqi.com/asia/",//亚洲
    "https://www.tianqi.com/europe/",//欧洲
    "https://www.tianqi.com/america/",//美洲
    "https://www.tianqi.com/oceania/",//大洋洲
    "https://www.tianqi.com/africa/"//非洲
]
let getCityBycoutury = couturys => {
    console.log(`开始获取${couturys.length}个国家的数据`)
    let responseJson = {}
    let arr = [];
    for (let i = 0; i < couturys.length-1; i++) {
        arr.push(axios.get(baseApi + couturys[i], { timeout: 30000 }))
    }
    axios.all(arr).then(axios.spread((...resps) => {
        console.log("国际城市全部获取成功")
        let $ = null;
        let count = 0;
        resps.forEach(resp => {
            $ = cheerio.load(resp.data)
            $("#citylist a").each((index, item) => {
                let a = $(item);
                responseJson[a.text()] = a.attr("href")
                count++;
            })
        })
        fs.writeFile(path.join(__dirname, "data/internationalCity.json"), JSON.stringify(responseJson), err => {
            if (err) {
                console.log(err)
                return
            }
            console.log(`${count}个国际城市持久化到本地！`)
        })
    })).catch(err => {
        console.log("错误")
        console.log(err)
    })

}

//获取国内城市
console.log("开始获取国内城市")
axios.get(chinacityApi).then(resp => {
    //计数器计算国内城市数
    let count = 0;
    let $ = cheerio.load(resp.data)
    let citybox = $(".citybox");
    citybox.find("a").each((i, item) => {
        let a = $(item)
        city[a.text()] = a.attr("href").split("/")[1]+"/"
        count++;
    })
    fs.writeFile(path.join(__dirname, "data/city.json"), JSON.stringify(city), err => {
        if (err) {
            console.log(err)
            return
        }
        console.log(`国内城市抓取成功！共抓取${count}条记录，并持久化到本地！`)
    })
})

let arr = []
for (let i = 0; i < apiArray.length; i++) {
    arr.push(axios.get(apiArray[i]))
}
console.log("开始获取各个大洲的国家")
axios.all(arr).then(axios.spread((res1, res2, res3, res4, res5) => {
    let resArr = [res1, res2, res3, res4, res5]
    //所有的国家
    let couturys = []
    resArr.forEach(res => {
        let $ = cheerio.load(res.data)
        $(".chooseBox a").each((index, item) => {
            let a = $(item)
            var coutury = a.attr("href").split("/")[1]
            if (coutury == "coutury_coutury_kenya") {
                coutury = "coutury_kenya"
            }
            couturys.push(coutury)
        })
    })
    
    fs.writeFile(path.join(__dirname, "data/couturys.json"), JSON.stringify(couturys), err => {
        if (err) {
            console.log(err)
            return
        }
        console.log(`国外国家名称持久化到本地！`)
    })
    getCityBycoutury(couturys)
}));